
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael K?lling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    private Thing thing;
    private Monster monster;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room curtain, volcano, pub, office,room1,room2,room3,lotus_pond,Bhikkhu,room2_e,
        room3_e,room3_w,room4,Sanctum;
      
        room1 = new Room("水帘洞");
        curtain = new Room("东海龙宫\n打败东海龙王可进入东海圣地获得龙",new Monster("东海龙王",2,10));
        Sanctum = new Room("东海圣地",new Thing("龙珠"));
        volcano = new Room("火焰山\n消灭这些魔祖的手下，打击魔祖的士气",new Monster("红孩儿",2,13));
        room2 = new Room("雾影山脉\n此时离无天的老巢越来越近了，我们更加迫切的需要去提升自己的实力，希望能够早提还三界一个安定");
        Bhikkhu = new Room("比丘国\n此处有妖怪，一切小心！！",new Monster("黄袍怪",1,9));
        room2_e =new Room("冥地",new Monster("地狱三头犬",2,8));
        room3 = new Room("暗影殿\n已经到了魔祖的老巢外围了，再通过息影魔山就是魔祖的老巢",new Monster("魔子",3,18));
        room3_w = new Room("息影魔山\n打败魔龙，便可进入魔族圣地",new Monster("守护魔龙",7,16));
        room3_e = new Room("神秘山洞\n这里有击败无天的至关重要的秘宝",new Thing("舍利"));
        room4=new Room("魔族圣地",new Monster("无天",15,200));
        
        
        room1.setExit("east", curtain);
        room1.setExit("north", room2);
        room1.setExit("west", volcano);
        curtain.setExit("west",room1);
        curtain.setExit("east",Sanctum);
        Sanctum.setExit("west",curtain);
        volcano.setExit("east", room1);
        room2.setExit("west", Bhikkhu);
        room2.setExit("east", room2_e);
        Bhikkhu.setExit("east",room2);
        room2_e.setExit("west",room2);
        room2.setExit("north", room3);
        room3.setExit("west", room3_w);
        room3.setExit("east", room3_e);
        room3_e.setExit("west",room3);
        room3_w.setExit("east",room3);
        room3_e.setExit("north",room4);

        currentRoom = room1;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("Thank you for playing.  Good bye.");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("梦幻西游世界!");
        System.out.println("这个世界是区别于西游记里面的世界，这里的角色都以发生翻天覆地的变化。");
        System.out.println("你所遇到的人和神仙是好是坏都不是有西游记里面来断定的。");
        System.out.println("有些神仙早已经是魔祖无天的手下。");
        System.out.println("他们企图推翻这个和平的世界，让魔族一统三界。");
        System.out.println("现在你是拥有大神通的孙悟空，你的任务就是找到无天魔祖，击败他");
        System.out.println("目前你生活在自己的水帘洞，你需要通过一步步的探寻来找到无天");
        System.out.println("想要击败无天，我们需要击败无天的各个手下才行，他们的手中都掌握这无天的本名精元，如若不然，无天还是可以通过散落的本名精元复活");
        System.out.println("在寻找的过程中你会寻到秘宝，此秘宝是击败无天的关键。");
        System.out.println("同时你还需要通过不断与无天的手下进行战斗来提升自己的战斗力。");
        System.out.println("在这场腥风血雨的斗争中提升自己，击败无天，成为三界的救世主吧！！！");
        System.out.println("You are " + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("命令错误");
            return false;
        }

        Word commandWord = command.getCommandWord();
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case PICK:
                pickThing();
                break;
            case LOOK:
                lookRoom();
                break;
            case CHECK:
                player.checkBag();
                break;
            case EAT:
                eatThing(command);
                break;
        }
        if(currentRoom.getDescription() == "魔族圣地"&&player.gethealth()>0) {
            wantToQuit=true;
            System.out.println("恭喜你！成功击败了无天，给三界带来了和平，成为了绝世大英雄");
            System.out.println("游戏结束！");
        }
        return wantToQuit;
     }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("没想到你孙悟空也需要帮助啊！");
        System.out.println("那我就看在苍生的面子上告诉你吧");
        System.out.println();
        System.out.println("你可以输入的命令有");
        System.out.println("   go quit help look check pick eat");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪？");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);

        if (nextRoom == null) {
            System.out.println("无法进入！");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你在 " + currentRoom.getDescription());
            if(currentRoom.getMonster()!=null)
            {
                currentRoom.getMonster().Pk(player);
            }
            currentRoom.printExits();
        }
    }
    
    private void lookRoom()
    {
        if(currentRoom.getThing()==null)
            System.out.println("这里没东西");
        else System.out.println("这里有"+currentRoom.getThing().getName());
    }
    
    private void pickThing()
    {
        player.pick(currentRoom.getThing());
        currentRoom.remove();
    }
    
    private void eatThing(Command command)
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("吃什么东西？");
            return;
        }
        player.eat(command.getSecondWord());
    }
    private void useThing(Command command)
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("使用什么东西？");
            return;
        }
        player.use(command.getSecondWord());
    }
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("退出什么?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}